﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;


namespace frmViewItemCatalog
{
    public partial class RequisitionForm : System.Web.UI.Page
    {
        LogicUniversityT2Entities context = new LogicUniversityT2Entities();

        ItemClass itemDetailForGridView = new ItemClass();
        ArrayList itemListArray = new ArrayList();
        ItemCatalog catalog = new ItemCatalog();
        string strGrandTotal;
        double subTotal=0.0;
        protected void Page_Load(object sender, EventArgs e)
        {

            TextBox userName = (TextBox)Page.FindControl("txtUserName");
            TextBox password = (TextBox)Page.FindControl("txtPassword");

            string requisitionID = context.EmployeeRequisitions.First().EmployeeRequisitionID.ToString();
            string surfixOfRequisitionID = requisitionID.Substring(0, 4);
            string numPartOfRequisitionID = requisitionID.Substring(2);
            int numID = int.Parse(numPartOfRequisitionID) + 1;
            string newNumPartOfRequisitionID = Convert.ToString(numID);
            if (!IsPostBack)
            {

                lblDate.Text = DateTime.Today.ToShortDateString();
                lblRequisitionID.Text = surfixOfRequisitionID + newNumPartOfRequisitionID;
                var c = (from invertoryUser in context.InventoryUsers
                         join department in context.Departments on invertoryUser.DepartmentID equals department.DepartmentID
                         join collectionPoint in context.CollectionPoints on department.CollectionPointID equals collectionPoint.CollectionPointID
                         where invertoryUser.Name.Equals("rekha") && invertoryUser.Password.Equals("password")
                         select new
                         {
                             departName = department.DepartmentName,
                             email = invertoryUser.Email,
                             loginName = invertoryUser.Name,
                             loginID = invertoryUser.EmployeeID,
                             collectionPoint = collectionPoint.CollectionPoint1
                         });
                foreach (var item in c)
                {
                    lblDepartmentName.Text = item.departName.ToString();
                    lblEmployeeID.Text = item.loginID.ToString();
                    lblEmployeeName.Text = item.loginName.ToString();
                    lblEmail.Text = item.email.ToString();
                    lblCollectionPoint.Text = item.collectionPoint.ToString();

                }
                cboCategoryName.DataTextField = "CategoryName";
                cboCategoryName.DataValueField = "CategoryID";
                cboCategoryName.DataSource = context.Categories;
                cboCategoryName.DataBind();
                string selectedCategory = cboCategoryName.SelectedItem.ToString();
                string selectedCategoryValue = cboCategoryName.SelectedValue.ToString();
                itemDetailForGridView.CategoryName = selectedCategory;
                var itemList = from itemDetail in context.ItemDetails
                               join item in context.Items on itemDetail.ItemID equals item.ItemID
                               where item.CategoryID == selectedCategoryValue
                               select new
                               {
                                   itemName = item.ItemDescription,
                                   itemNum = item.ItemID,
                                   itmePrice = itemDetail.Price
                               };

                ArrayList itemName = new ArrayList();

                foreach (var list in itemList)
                {
                    itemDetailForGridView.ItemName = list.itemName;
                    itemDetailForGridView.ItemPrice = Double.Parse(list.itmePrice);
                    itemDetailForGridView.ItemID = list.itemNum;
                    itemName.Add(itemDetailForGridView);

                }

                ArrayList itemNameInddl = new ArrayList();
                foreach (ItemClass list in itemName)
                {

                    itemNameInddl.Add(list.ItemName);
                }
                cboItemName.DataSource = itemNameInddl;
                cboItemName.DataBind();
                itemDetailForGridView.OrderQuantity = int.Parse(txtOrderQuantity.Text.ToString());
                catalog.Add(itemDetailForGridView);

                Session["itemDetail"] = catalog;

            }
        }

        protected void btnAddOrder_Click(object sender, EventArgs e)
        {

            catalog = (ItemCatalog)Session["itemDetail"];
            strGrandTotal = (string)Session["Total"];
            GridViewOrderList.DataSource = catalog.ListOfItem;
            GridViewOrderList.DataBind();
            string selectedCategory = cboCategoryName.SelectedValue.ToString();
            var itemList = from itemDetail in context.ItemDetails
                           join item in context.Items on itemDetail.ItemID equals item.ItemID
                           where item.CategoryID == selectedCategory
                           select new
                           {
                               itemName = item.ItemDescription,
                               itemNum = item.ItemID,
                               itmePrice = itemDetail.Price
                           };


            ArrayList itemName = new ArrayList();

            foreach (var list in itemList)
            {

                itemDetailForGridView.ItemName = list.itemName;
                itemDetailForGridView.ItemPrice = Double.Parse(list.itmePrice);
                itemDetailForGridView.ItemID = list.itemNum;

                itemName.Add(itemDetailForGridView);


            }
            ArrayList itemNameInddl = new ArrayList();

            foreach (ItemClass list in itemName)
            {
                itemNameInddl.Add(list.ItemName);
            }

            itemDetailForGridView.CategoryName = cboCategoryName.SelectedItem.ToString();
            itemDetailForGridView.ItemName = cboItemName.SelectedItem.ToString();
            itemDetailForGridView.OrderQuantity = int.Parse(txtOrderQuantity.Text.ToString());
            catalog.Add(itemDetailForGridView);
            Session["itemDetail"] = catalog;
            // itemDetailForGridView.ItemPrice=
            // var purchaselist =from itemDetail in logic.ItemDetails
            //                   join item in logic.Items on itemDetail.ItemID equals item.ItemID
            //                   join supplier in logic.Suppliers on itemDetail.SupplierID equals supplier.SupplierID
            //                  where itemDetail.Item.StockBalance< itemDetail.Item.ReorderLevel
            //                  select new{
            //                  ItemNo = itemDetail.ItemID,
            //                  ItemDescription=itemDetail.Item.ItemDescription,
            //                  Quantity="",
            //                  Price=itemDetail.Price,
            //                  Amount=""};


            //var plist=purchaselist.ToList();
            //dglist.DataSource = plist;


        }

        protected void btnSendForApproval_Click(object sender, EventArgs e)
        {
        //                for (int i = 0; i < gvItemList.Rows.Count; i++)
        //    {
        //        DataGridViewCheckBoxCell chbox1 = new DataGridViewCheckBoxCell();

        //        chbox1 = (DataGridViewCheckBoxCell)gvItemList.Rows[i].Cells[0];
        //        if (chbox1.Value == null)
        //            chbox1.Value = false;
        //        if (chbox1.Value.ToString() == "")
        //        {
        //            chbox1.Value = true;
        //        }
        //        if (Convert.ToBoolean(chbox1.Value) == true)
        //        {
        //            AdjustmentVoucher adjVoucher = new AdjustmentVoucher();

        //            int strAdjID = (logic.AdjustmentVouchers.Count())-1;
        //            var a = from adid in logic.AdjustmentVouchers select adid;
        //            adjVoucher = a.AsEnumerable().ElementAt<AdjustmentVoucher>(strAdjID);
        //            string stringa = adjVoucher.AdjustmentID;
        //            string surfixOfAdjID = stringa.Substring(0, 4);
        //            string numPartOfAdjID = stringa.Substring(2);
        //            int numID = int.Parse(numPartOfAdjID) + 1;
        //            string newNumPartOfAdjID = Convert.ToString(numID);
        //            string combineofSurfixNNum = surfixOfAdjID + newNumPartOfAdjID;

        //            AdjustmentVoucher newAdjVoucher = new AdjustmentVoucher();
        //            newAdjVoucher.AdjustmentID = combineofSurfixNNum.ToString();
        //            DataGridViewTextBoxCell txtbox1 = new DataGridViewTextBoxCell();
        //            txtbox1 = (DataGridViewTextBoxCell)gvItemList.Rows[i].Cells[1];

        //            newAdjVoucher.ItemID = txtbox1.Value.ToString();
        //            newAdjVoucher.Date = DateTime.Today;
        //            newAdjVoucher.Quantity = 0;
        //            newAdjVoucher.Status = "Pending";
        //            newAdjVoucher.Remarks = "Null";
        //            logic.AddToAdjustmentVouchers(newAdjVoucher);
        //            logic.SaveChanges();
            if (Session["itemDetail"]!=null)
            {
                catalog = (ItemCatalog)Session["itemDetail"];

            }
                

        }

        protected void GridViewOrderList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);

            if (Session["itemDetail"] != null)
            {
                catalog = (ItemCatalog)Session["itemDetail"];

            }

            if (e.CommandName == "Remove")
            {

                catalog.ListOfItem.RemoveAt(index);

                GridViewOrderList.DataSource = catalog.ListOfItem;
                GridViewOrderList.DataBind();
            }
        }

        protected void GridViewOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            double  total = 0.0 ;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                TextBox qty = (TextBox)e.Row.FindControl("txtOrderQuantity");
                Label Total = (Label)e.Row.FindControl("lblTotal");
                Label lblPrice = (Label)e.Row.FindControl("lblItemPrice");

                string a = lblPrice.Text.Replace("$", "");
                double b = double.Parse(a);
                double c = Convert.ToDouble(qty.Text);

                 total = b * c;
                Total.Text = "$" + Convert.ToString(total);
                subTotal += total;
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
               // Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice");
                Label lblGrandTotal = (Label)e.Row.FindControl("lblGrandTotal");
                //lblTotalPrice.Text = "SubTotal:$" + subtotal;
                string strGrandTotal = Convert.ToString(subTotal);
                lblGrandTotal.Text = "GrandTotal:$" + strGrandTotal;
                Session["Total"] = strGrandTotal;
                
            }
        }

        protected void cboCategoryName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedCategory = cboCategoryName.SelectedValue.ToString();
            var itemList = from itemDetail in context.ItemDetails
                           join item in context.Items on itemDetail.ItemID equals item.ItemID
                           where item.CategoryID == selectedCategory
                           select new
                           {
                               itemName = item.ItemDescription,
                               itemNum = item.ItemID,
                               itmePrice = itemDetail.Price
                           };


            ArrayList itemName = new ArrayList();

            foreach (var list in itemList)
            {

                itemDetailForGridView.ItemName = list.itemName;
                itemDetailForGridView.ItemPrice = Double.Parse(list.itmePrice);
                itemDetailForGridView.ItemID = list.itemNum;

                itemName.Add(itemDetailForGridView);


            }
            ArrayList itemNameInddl = new ArrayList();

            foreach (ItemClass list in itemName)
            {

                itemNameInddl.Add(list.ItemName);

            }

            cboItemName.DataSource = itemNameInddl;
            cboItemName.DataBind();

            
        }




    }
}